<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRole } from '@/api/settings'
import { getUserProfile } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 用来存储选中的
      list: [] // 存储获取的角色权限列表信息
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    // 更新分配角色
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.roleId, roleIds: this.roleIds })
        this.$message.success(res.message)
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取角色权限信息和数据回显
    async loadRole() {
      try {
        const res = await getRole()
        console.log('getRole', res)
        // 保存数据
        this.list = res.data.rows
        // console.log(this.roleId)
        const info = await getUserProfile(this.roleId)
        console.log(info)
        this.roleIds = info.data.roleIds
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
